package exam.card.service.impl;

import java.util.List;
import java.util.Map;

import exam.bank.dao.BankDao;
import exam.bank.dao.impl.BankDaoImpl;
import exam.bank.entity.BankEntity;
import exam.card.dao.CardDao;
import exam.card.dao.impl.CardDaoImpl;
import exam.card.entity.CardEntity;
import exam.card.service.CardService;

/**
 * @author jzh
 * @创建时间 2017年6月17日下午4:11:11 类说明：
 */
public class CardServiceImpl implements CardService {

	CardDao cardDao = new CardDaoImpl();
	BankDao bankDao = new BankDaoImpl();

	@Override
	public void addCard(String cardName, String cardNumber, String cardChip, String cardMoney, String cardHolder,
			String bankId, Map<String, Object> mp) {
		CardEntity CardEntity = new CardEntity();
		CardEntity.setCardName(cardName);
		CardEntity.setCardNumber(cardNumber);
		int chip=Integer.parseInt(cardChip);
		CardEntity.setCardChip(chip);
		double money = Double.parseDouble(cardMoney);
		CardEntity.setCardMoney(money);
		CardEntity.setCardHolder(cardHolder);
		int id = Integer.parseInt(bankId);
		CardEntity.setBankId(id);

		try {
			int counter = cardDao.addCard(CardEntity);
			// mp.put("CardEntity", CardEntity);
			mp.put("result", "增加成功");
		} catch (Exception e) {
			mp.put("result", "增加失败");
		}
	}

	@Override
	public void findAllCard(String cur, String ps, Map<String, Object> mp) {
		// 非空检查，并赋默认值
		if (null == cur || "".equals(cur)) {
			cur = "1";
		}
		int curpage = Integer.parseInt(cur);
		if (null == ps || "".equals(ps)) {
			ps = "3";
		}
		int pageSize = Integer.parseInt(ps);

		try {
			List<CardEntity> cardLists = cardDao.findAllCard();
			mp.put("cardLists", cardLists);
			mp.put("result", "查找成功");
		} catch (Exception e) {
			mp.put("result", "查找失败");
		}

	}

	@Override
	public void findCardById(String upStr, Map<String, Object> mp) {
		Integer upId=Integer.parseInt(upStr);
		try {
			CardEntity cardEntity = cardDao.findCardById(upId);
			mp.put("cardEntity", cardEntity);
			mp.put("result", "成功");
		} catch (Exception e) {
			mp.put("result", "失败 ");
		}
	}

	@Override
	public void updateCard(String id, String cardName, String cardNumber, String cardChip, String cardMoney,
			String cardHolder, String bankId, Map<String, Object> mp) {
		CardEntity cardEntity=new CardEntity();
		int cardId = Integer.parseInt(id);
		cardEntity.setId(cardId);
		cardEntity.setCardName(cardName);
		cardEntity.setCardNumber(cardNumber);
		int chip = Integer.parseInt(cardChip);
		cardEntity.setCardChip(chip);
		double money = Double.parseDouble(cardMoney);
		cardEntity.setCardMoney(money);
		cardEntity.setCardHolder(cardHolder);
		int bId = Integer.parseInt(bankId);
		cardEntity.setBankId(bId);
		
		try {
			cardDao.updateCard(cardEntity);
			mp.put("result", "修改成功");
		} catch (Exception e) {
			mp.put("result", "修改失败 ");
		}
	}

	@Override
	public void deleteCard(String id, Map<String, Object> mp) {
		Integer delId=Integer.parseInt(id);
		try {
			cardDao.deleteCard(delId);
			mp.put("result", "删除成功");
		} catch (Exception e) {
			mp.put("result", "删除失败 ");
		}
	}

	@Override
	public void findAllBank(Map<String, Object> mp) {
		try {
			List<BankEntity> bankLists = bankDao.findAllBank();
			mp.put("bankLists", bankLists);
			mp.put("result", "查找成功");
		} catch (Exception e) {
			mp.put("result", "查找失败");
		}
	}

}
